package objects.base;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

import stemming.tools.Tools;

public class DocumentAnalysisTools {
	public static Hashtable<String,Integer> hashDateCount = new Hashtable<String, Integer>();
	public static Hashtable<String,Set<Document>> hashDateAllDocument = new Hashtable<String, Set<Document>>();

	public static void cutDocumentByWord(Hashtable<String,WordData> hashStringToWord, Date date, Document document){
		if(hashStringToWord != null && document != null && !document.getStr().equals("") && !document.getStr().equals(" ")){
			StringTokenizer sTokenize = new StringTokenizer(document.getStr()," ");
			while (sTokenize.hasMoreTokens()) {
				String token = Tools.stem(utils.primitif.StringUtils.removeBadChar(sTokenize.nextToken().toLowerCase()));
				WordData wD;
				if(hashStringToWord.containsKey(token)){
					wD = hashStringToWord.get(token);
				}else{
					wD = new WordData(token);
				}
				wD.put(date, document);
				hashStringToWord.put(token, wD);
				//System.out.println("Currently: Analysis [" + token + "]");
			}
			putDate(date);
			putDocForDate(date,document);
		}else{
			System.err.println("Hashtable null - Critical error");
			System.exit(-1);
		}
	}

	public static void printHashTable(Hashtable<String,WordData> hashStringToWord){
		Iterator<WordData> itValue = hashStringToWord.values().iterator(); 
		while(itValue.hasNext()){ 
			WordData value = itValue.next();
			System.out.println(value.toString());
		} 
	}

	public static void putDate(Date date){
		String sDate = date.toString();
		if(hashDateCount.containsKey(sDate)){
			hashDateCount.put(sDate,hashDateCount.get(sDate).intValue() + 1);
		}else{
			hashDateCount.put(sDate,1);
		}
	}

	public static void putDocForDate(Date date, Document document){
		String sDate = date.toString();
		Set<Document> current;
		if(hashDateAllDocument.containsKey(sDate)){
			current = hashDateAllDocument.get(sDate);
		}else{
			current = new HashSet<Document>();
		}
		current.add(document);
		hashDateAllDocument.put(sDate,current);
	}

	@SuppressWarnings("unchecked")
	public static void cutDocumentByWordForHbase(HTable table,HTable tableDoc, Date date, Document document) throws IOException{
		if(document != null && !document.getStr().equals("") && !document.getStr().equals(" ")){
			byte[] dateFamily = Bytes.toBytes("date");
			byte[] col1 = Bytes.toBytes(date.toString());

			byte[] docFamily = Bytes.toBytes("doc");
			byte[] colDoc = Bytes.toBytes(String.valueOf(document.getNumber()));
			Put docPut = new Put(Bytes.toBytes(date.toString()));
			docPut.add(docFamily, colDoc, Bytes.toBytes(String.valueOf(document.getVoteOrOpinionCount())));
			tableDoc.put(docPut);
			
			StringTokenizer sTokenize = new StringTokenizer(document.getStr()," ");
			while (sTokenize.hasMoreTokens()) {
				ArrayList<Integer> doc = null;
				ByteArrayOutputStream ArrSer = new ByteArrayOutputStream();
				ObjectOutputStream obj_out = new ObjectOutputStream(ArrSer);


				String token = Tools.stem(utils.primitif.StringUtils.removeBadChar(sTokenize.nextToken().toLowerCase()));
				Put p = new Put(Bytes.toBytes(token));
				Get g = new Get(Bytes.toBytes(token));
				Result result = table.get(g);

				boolean exists = result.containsColumn(dateFamily, col1);
				if(exists){
					byte[] currentValue = table.get(g).getValue(dateFamily, col1);
					try {
						ByteArrayInputStream bis = new ByteArrayInputStream(currentValue);
						ObjectInputStream obj_in = new ObjectInputStream(bis);
						doc = (ArrayList<Integer>) obj_in.readObject();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					doc = new ArrayList<Integer>();
				}
				doc.add(document.getNumber());
				obj_out.writeObject(doc);
				p.add(dateFamily, col1, ArrSer.toByteArray());
				table.put(p);
				//table.incrementColumnValue(Bytes.toBytes(token), famA, col1, 1);
			}
		}else{
			System.err.println("Empty element - skip");
		}
	}
	
	public static void calculateFrequencies(HTable tableWord,HTable tableDoc){
		

	}
}
